#ifndef     __DRAW_TASK_H
#define     __DRAW_TASK_H

#include "../rtos/thread.h"
#include "../rtos/message_queue.h"
#include "../rtos/event_flags.h"

namespace ice_task {

class DrawTask : public rtos::Thread<DrawTask, 1024, osPriorityHigh1> {

public:
    static constexpr uint32_t DRAW_TIMEOUT = 30 * 60 * 1000;
    static constexpr uint32_t FLAG_STOP = 0x01;

    enum class Mode : uint8_t {
        IDLE = 0x00,
        WORK = 0x01,
        TIMEOUT = 0x02,
        STOP = 0x03,
    };

    void init_start();

    void run();

    void stop() {
        m_flags.clr_flags(FLAG_STOP);
        m_mq.post(Mode::STOP);
        m_flags.wait_any_flags(FLAG_STOP);
    }

    void idle() {
        m_mq.post(Mode::IDLE);
    }

    inline uint8_t state() {
        return static_cast<uint8_t>(m_mode);
    }

private:
    void do_idle();
    void do_work();
    void do_stop();
    void do_timeout();

private:
    inline bool wait(uint32_t timeout = osWaitForever) {
        return m_mq.poll(m_mode, timeout) == osOK;
    }

private:
    rtos::lazy::MessageQueue<Mode, 2> m_mq;
    rtos::lazy::EventFlags m_flags;
    Mode m_mode = Mode::IDLE;
};

extern DrawTask drawTask;

}


#endif
